package itsm.isperp.module.repository.solr;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.domain.IBean;
import itsm.isperp.module.utils.SolrUtils;

import java.lang.reflect.ParameterizedType;

import org.dom4j.DocumentException;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.solr.core.SolrOperations;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.HighlightOptions;
import org.springframework.data.solr.core.query.HighlightQuery;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.data.solr.repository.support.SimpleSolrRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

@NoRepositoryBean
public class BaseSolrRepository<T extends IBean> extends
		SimpleSolrRepository<T, String> {
	
 

	public void saveIndex(String id) throws DocumentException {
		JdbcTemplate jdbcTemplate = ContextHolder
				.getSpringBean(JdbcTemplate.class);

		DbConfigEntity dbConfigEntity = this.getClass().getAnnotation(
				DbConfigEntity.class);

		String name = dbConfigEntity.name();

		String sql = "SELECT * FROM (" + SolrUtils.getQueryDtoByName(name)
				+ ")T";

		@SuppressWarnings("unchecked")
		Class<T> clazz = (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];

		T dto = jdbcTemplate.queryForObject(sql + " WHERE id='" + id + "'",
				new BeanPropertyRowMapper<T>(clazz));

		this.save(dto);

	}

	/**
	 * 根据关键字查询
	 * 
	 * @param fullname
	 * @return
	 */
	public T findOne(String id) {
		SimpleQuery sq = new SimpleQuery();
		Criteria ca = new Criteria("id");
		ca.endsWith(id);
		sq.addCriteria(ca);

		SolrOperations so = getSolrOperations();

		@SuppressWarnings("unchecked")
		Class<T> clazz = (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];

		Criteria c = new Criteria("classType").is(clazz.getSimpleName());
		sq.addCriteria(c);
		T t = so.queryForObject(sq, clazz);
		return t;
	}

	/**
	 * 根据关键字查询
	 * 
	 * @param fullname
	 * @return
	 */
	public PageResponse<T> searchByKeyword(HighlightQuery query) {

		SolrOperations so = getSolrOperations();
		DbConfigEntity dbConfigEntity = this.getClass().getAnnotation(
				DbConfigEntity.class);
		String[] hlfield = dbConfigEntity.hlfields();

		if (!query.hasHighlightOptions()) {
			HighlightOptions op = new HighlightOptions();
			op.addField(hlfield);
			op.setFragsize(50);
			op.addHighlightParameter("hl.alternateField", "true");
			op.addHighlightParameter("hl.maxAlternateFieldLength", "50");

			query.setHighlightOptions(op);
		}

		@SuppressWarnings("unchecked")
		Class<T> clazz = (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];

		Criteria c = new Criteria("classType").is(clazz.getSimpleName());
		query.addCriteria(c);
		HighlightPage<T> re = so.queryForHighlightPage(query, clazz);

		return SolrUtils.genericHighlightPageResponse(re);
	}
}
